package com.lzp.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lzp.entity.TbSpecification;
import com.lzp.entity.TbSpecificationExample;
import com.lzp.mapper.TbSpecificationMapper;
import com.lzp.service.TbSpecificationService;
import com.lzp.util.PageResult;
import com.lzp.vo.TbSpecificationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class TbSpecificationServiceImpl implements TbSpecificationService {
    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;
    @Override
    public List<TbSpecification> findAll() {
        List<TbSpecification> tbSpecifications = tbSpecificationMapper.selectByExample(null);
        return tbSpecifications;
    }

    @Override
    public PageResult<TbSpecification> findByPage(Integer page, Integer pageSize) {
        //分页函数调用
        PageHelper.startPage(page,pageSize);
        //查询所有的的分类
        List<TbSpecification> tbSpecifications = this.findAll();

        Page<TbSpecification> tbSpecificationsPage = (Page<TbSpecification>) tbSpecifications;
        return new PageResult<>(tbSpecificationsPage.getResult(),tbSpecificationsPage.getTotal());
    }

    @Override
    public PageResult<TbSpecification> search(Integer page, Integer pageSize, TbSpecificationVO tbSpecificationVO) {
        // 开始分页
        PageHelper.startPage(page, pageSize);

        TbSpecificationExample example = new TbSpecificationExample();
        // 添加查询条件到查询实例中
        TbSpecificationExample.Criteria criteria = example.createCriteria();
        if(StrUtil.isNotBlank(tbSpecificationVO.getSpecName())){
            criteria.andSpecNameLike("%" + tbSpecificationVO.getSpecName() + "%");
        }

        List<TbSpecification> tbSpecification = tbSpecificationMapper.selectByExample(example);
        // 2.4 将上面的tb对象转换为List集合
        Page<TbSpecification> tbSpecificationsPage = (Page<TbSpecification>) tbSpecification;
        return new PageResult<>(tbSpecificationsPage.getResult(),tbSpecificationsPage.getTotal());
    }

    @Override
    public boolean save(TbSpecification tbSpecification) {
        return tbSpecificationMapper.insert(tbSpecification) > 0;
    }
}
